import { defineStore  } from 'pinia'
import {loginAPI } from '@/api/user.js'
import { ElMessage  } from 'element-plus'
import {setToken, getToken, removeToken, getUser, setUser, removeUser } from '@/utils/localUserInfo.js'
 
const userStore = defineStore('user', {
    state: () => {
        return {
           dialogShow: false, // 控制登录框显示隐藏
        //    用户信息 
           userInfo: getUser() || {} ,
           token: getToken()  || '' ,
        }
    },
    actions: {
        // 改变登录弹框状态
       setDialogShow(val) {
        this.dialogShow = val
       },
        // 手机账号登录
       async getLogin(data) {
            const res = await loginAPI(data)
            // console.log(res);
            if (res.code == 200) {
                // 提示用户
                ElMessage.success('登录成功')
                this.token = res.data.token
                this.userInfo.name = res.data.name
                setToken(res.data.token)
                setUser({name: res.data.name})
            } else {
                ElMessage.success('登录失败')
            }
            return res
       },
        // 退出登陆
       async loginOut() {
           this.token = ''
           this.userInfo = {}
           removeToken()
           removeUser()
       }

    },
    getters: {

    }

},{
    persist: true
})

export default userStore